<?php
/**
 * @author spenceryg
 * @date 2022/11/27
 * @time 12:55 下午
 * @focus love & life
 */


namespace app\auto\controller;


use app\auto\business\FieldBusiness;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Request;
use think\facade\View;
use think\response\Json;

class Field extends \app\BaseController
{
    /**
     * add
     * @return string|Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function add()
    {
        if(request()->isPost()){
            $fields = ['name', 'comment', 'type', 'length', 'default','is_null', 'table_id'];
            $data = request()->post($fields);
            $field = FieldBusiness::add($data);
            return success('添加字段成功', $field);
        }else{
            return View::fetch();
        }
    }

    /**
     * update
     * @return string|Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function update()
    {
        if(request()->isPost()){
            $fields = ['id','name', 'comment', 'type', 'length', 'default','is_null', 'table_id'];
            $data = request()->post($fields);
            $field = FieldBusiness::update($data);
            return success('更新字段成功', $field);
        }else{
            $r = \app\auto\model\Field::find(input('id'));
            View::assign('r', $r);
            return View::fetch();
        }
    }


    /**
     * fastAdd
     * @return string|Json
     */
    public function fastAdd()
    {
        if(request()->isPost()){
            $data = input();
            foreach ($data['name'] as $key=>$item) {
                $this_data = [
                    'name' => $data['name'][$key],
                    'comment' => $data['comment'][$key],
                    'type' => $data['type'][$key],
                    'length' => $data['length'][$key],
                    'default' => $data['default'][$key],
                    'is_null' => $data['is_null'][$key],
                    'table_id' => $data['table_id'],
                ];
                if($this_data['name']){
                    try {
                        FieldBusiness::add($this_data);
                    } catch (\Exception $e) {
                        continue;
                    }
                }
            }
            return success();
        }else{
            return View::fetch();
        }

    }


    /**
     * index
     * @return string
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function index(): string
    {
        $table_id = input('table_id');
        $data = \app\auto\model\Field::where('table_id', $table_id)->order('sort asc,id asc')->select();
        View::assign('data', $data);
        return View::fetch();
    }


    /**
     * delete
     * @return Json
     */
    public function delete(): Json
    {
        $id = input('id');
        \app\auto\model\Field::destroy($id);
        return success('删除字段成功!');
    }

    public function config()
    {
        if(request()->isPost()){
            $id = input('id');
            $data = input();
            unset($data['id']);
            $update['id'] = $id;
            $update['config'] = $data;
            \app\auto\model\Field::update($update);
            return success();
        }else{
            $id = input('id');
            $r = \app\auto\model\Field::find($id);
            View::assign('r', $r['config'] );
            return View::fetch();
        }
    }
}
